package com.sxkj.web.controller.sys;

import com.sxkj.common.response.ResponseResult;
import com.sxkj.entity.base.Page;
import com.sxkj.entity.entity.sys.Role;
import com.sxkj.entity.entity.sys.RoleMenu;
import com.sxkj.service.sys.RoleMenuService;
import com.sxkj.service.sys.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @title: RoleController
 * @author: daixiaowei
 * @date: 2019年12月19日 下午 01:51:50
 * @description: 角色API
 */
@Controller
@Api(tags = "RoleController", description = "角色接口")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;


    /**
     *
     * @title: searchRoleListPage
     * @author: daixiaowei
     * @date: 2019年12月19日 下午 01:56:29
     * @description: 查询角色列表
     * @param: name pageSize pageNum
     * @return:
     */
    @ApiOperation(value = "查询角色列表")
    @RequestMapping(value = "/searchRoleListPage", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult searchRoleListPage(@RequestParam(value =
            "name") String name, @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return  ResponseResult.success(Page.restPage(roleService.searchListPage(name,pageSize,pageNum)));
    }

    /**
     *
     * @title: roleAdd
     * @author: daixiaowei
     * @date: 2019年12月21日 下午 02:24:19
     * @description: 角色添加接口
     * @param: role
     * @return:
     */
    @ApiOperation(value = "角色添加")
    @RequestMapping(value = "/roleAdd", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult roleAdd(@RequestBody Role role, BindingResult result) {
        roleService.roleAdd(role);
        return ResponseResult.success();
    }

    /**
     *
     * @title: searchRoleMenu
     * @author: daixiaowei
     * @date: 2019年12月21日 下午 03:35:58
     * @description: 查询roleMenu
     * @param: roleId
     * @return:
     */
    @ApiOperation(value = "查询roleMenu")
    @RequestMapping(value = "/searchRoleMenu", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult searchRoleMenu(@RequestParam(value =
            "roleId") String roleId) {
        return  ResponseResult.success(roleMenuService.searchRoleMenuByRoleId(roleId));
    }


    /**
     *
     * @title: editRole
     * @author: daixiaowei
     * @date: 2019年12月21日 下午 03:49:56
     * @description: 角色修改
     * @param: role
     * @return:
     */
    @ApiOperation(value = "角色修改")
    @RequestMapping(value = "/editRole", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult editRole(@RequestBody Role role, BindingResult result) {
        roleService.roleUpdate(role);
        return ResponseResult.success();
    }

    /**
     *
     * @title: deleteRole
     * @author: daixiaowei
     * @date: 2019年12月21日 下午 04:14:41
     * @description: 角色删除
     * @param: map
     * @return:
     */
    @ApiOperation(value = "角色删除")
    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult deleteRole(@RequestBody Map map, BindingResult result) {
        roleService.deleteByUserId(map.get("id").toString());
        return ResponseResult.success();
    }

    /**
     *
     * @title: roleList
     * @author: daixiaowei
     * @date: 2019年12月24日 上午 11:56:49
     * @description: 重新加载role
     * @param:
     * @return:
     */
    @ApiOperation(value = "重新加载role")
    @RequestMapping(value = "/roleList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult roleList() {
        return ResponseResult.success(roleService.searchList());
    }
}
